//Copyright (c) 2010 Jimmy Merari - jim.merari@gmail.com

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.


using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using DotNetNuke.Framework.Providers;

namespace Cinovasi.Scriptagility
{
    public class SqlDataProvider : DataProvider
    {
        private const string providerType = "data";

        #region Private Members
        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(providerType);
        private string _connectionString;
        private string _providerPath;
        private string _objectQualifier;
        private string _databaseOwner;
        #endregion

        #region Constructors
        public SqlDataProvider()
        {
            Provider objProvider = ((Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);
            if (objProvider.Attributes["connectionStringName"] != "" && System.Configuration.ConfigurationManager.AppSettings[objProvider.Attributes["connectionStringName"]] != "")
            {
                _connectionString = System.Configuration.ConfigurationManager.AppSettings[objProvider.Attributes["connectionStringName"]];
            }
            else
            {
                _connectionString = objProvider.Attributes["connectionString"];
            }
            _providerPath = objProvider.Attributes["providerPath"];
            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false)
            {
                _objectQualifier += "_";
            }
            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false)
            {
                _databaseOwner += ".";
            }
        }
        #endregion

        #region Properties
        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
        }

        public string ProviderPath
        {
            get
            {
                return _providerPath;
            }
        }

        public string ObjectQualifier
        {
            get
            {
                return _objectQualifier;
            }
        }

        public string DatabaseOwner
        {
            get
            {
                return _databaseOwner;
            }
        }
        #endregion

        #region Private Methods
        private object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        #endregion

        #region MiddleForm Methods

        public override int SaveScript(int scriptId, int moduleId, string scriptName,
            int placement, string script, bool enableTokenReplace, string contentType,
            int scriptOrder)
        {
            object result = SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "Cinovasi_Scriptagility_SaveScript",
                scriptId, moduleId, scriptName, placement, script, enableTokenReplace,
                contentType, scriptOrder);

            return int.Parse(result.ToString());
        }

        public override IDataReader GetScriptByModuleId(int moduleId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Cinovasi_Scriptagility_GetScriptByModuleId", moduleId);
        }

        public override IDataReader GetExternalScriptByModuleId(int moduleId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Cinovasi_Scriptagility_GetExternalScriptByModuleId", moduleId);
        }

        public override IDataReader GetScriptById(int scriptId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Cinovasi_Scriptagility_GetScriptById", scriptId);
        }

        public override IDataReader GetExternalScriptByName(int scriptId, string name)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Cinovasi_Scriptagility_GetExternalScriptByName", scriptId, name);
        }

        public override void DeleteScript(int scriptId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "Cinovasi_Scriptagility_DeleteScript", scriptId);
        }

        #endregion


    }
}
